from wtforms import Form, StringField, PasswordField
from wtforms.validators import DataRequired, Length, Email, ValidationError, EqualTo
from app.models.user import User


class RegisterForm(Form):
    """
    用户注册
    """
    email = StringField(
        validators=[
            DataRequired(),
            Length(min=8, max=64),
            Email('邮箱不符合规范')
        ]
    )
    password = PasswordField(
        validators=[
            DataRequired(message='密码不可以为空，请输入你的密码'), Length(6, 32)
        ]
    )
    nickname = StringField(
        validators=[
            DataRequired(), Length(2, 10, message='昵称至少需要两个字符，最多10个字符')
        ]
    )

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('邮箱账号已被注册')


class LoginForm(Form):
    """
    用户登录
    """
    email = StringField(
        validators=[
            DataRequired(),
            Length(min=8, max=64),
            Email('邮箱不符合规范')
        ]
    )
    password = PasswordField(
        validators=[
            DataRequired(message='密码不可以为空，请输入你的密码'), Length(6, 32)
        ]
    )


class EmailForm(Form):
    """
    发送邮件
    """
    email = StringField(
        validators=[
            DataRequired(),
            Length(min=8, max=64),
            Email('邮箱不符合规范')
        ]
    )

    def validate_email(self, field):
        if not User.query.filter_by(email=field.data).first():
            raise ValidationError('邮箱账号未注册')


class ResetPasswordForm(Form):
    """
    重置密码
    """
    password1 = PasswordField(
        validators=[
            DataRequired(message='密码不可以为空，请输入你的密码'),
            Length(6, 32),
            EqualTo('password2', message='再次密码输入不一致')
        ]
    )
    password2 = PasswordField(
        validators=[
            DataRequired(message='确认密码不可以为空，请输入你的密码'),
            Length(6, 32),
        ]
    )
